package com.fs.system.college_admin.service;

import com.fs.system.college_admin.mapper.NoticeMapper;
import com.fs.system.exception.CustomerException;
import com.fs.system.college_admin.domain.Notice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class NoticeService {
    
    @Resource
    private NoticeMapper noticeMapper;

    public void addNotice(Notice notice) {
        notice.setDeleted(false);
        notice.setCreateTime(LocalDateTime.now());
        noticeMapper.addNotice(notice);
    }

    public List<Notice> selectNoticeList(Notice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    public PageInfo<Notice> selectPage(Notice notice, Integer pageNum, Integer pageSize) {
        //开启分页查询（传参：当前页码、每页分页个数）
        PageHelper.startPage(pageNum,pageSize);
        //查询所有的数据
        List<Notice> noticeList = noticeMapper.selectNoticeList(notice);
        //PageInfo执行分页操作
        return PageInfo.of(noticeList);
    }

    public void updateNotice(Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.updateNotice(notice);
    }

    /**
     * 单个删除
     * @param noticeId：需修改的id
     * @param updateBy：修改操作的人
     */
    public void deleteNotice(Long noticeId, String updateBy) {
        Notice dbNotice = noticeMapper.selectNoticeById(noticeId);
        if(Objects.isNull(dbNotice)){
            throw new CustomerException("500","删除失败");
        }
        dbNotice.setDeleted(true);
        dbNotice.setUpdateBy(updateBy);
        dbNotice.setUpdateTime(LocalDateTime.now());
        noticeMapper.updateNotice(dbNotice);
    }

    /**
     * 批量删除
     * @param updateBy
     * @param ids
     */
    public void deleteBatch(List<Long> ids,String updateBy) {
        for (Long noticeId :ids) {
            this.deleteNotice(noticeId, updateBy);
        }
    }
}
